/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain (a) copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.ohos.core;

import ohos.app.Context;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;

import io.sentry.ILogger;
import io.sentry.OptionsContainer;
import io.sentry.Sentry;
import io.sentry.SentryLevel;

/**
 * Sentry initialization class
 *
 * @since 2021-06-01
 */
public final class SentryOhos {
    private SentryOhos() {
    }

    /**
     * Sentry initialization method if auto-init is disabled
     *
     * @param context Application. context
     */
    public static void init(@NotNull final Context context) {
        init(context, new OhosLogger());
    }

    /**
     * Sentry initialization with (a) custom logger
     *
     * @param context Application. context
     * @param logger your custom logger that implements ILogger
     */
    public static void init(@NotNull final Context context, @NotNull ILogger logger) {
        init(context, logger, options -> {
        });
    }

    /**
     * Sentry initialization with (a) configuration handler that may override the default options
     *
     * @param context Application. context
     * @param configuration Sentry.OptionsConfiguration configuration handler
     */
    public static void init(
        @NotNull final Context context,
        @NotNull Sentry.OptionsConfiguration<SentryOhosOptions> configuration) {
        init(context, new OhosLogger(), configuration);
    }

    /**
     * Sentry initialization with (a) configuration handler and custom logger
     *
     * @param context Application. context
     * @param logger your custom logger that implements ILogger
     * @param configuration Sentry.OptionsConfiguration configuration handler
     * @throws RuntimeException
     */
    public static void init(
        @NotNull final Context context,
        @NotNull ILogger logger,
        @NotNull Sentry.OptionsConfiguration<SentryOhosOptions> configuration) {
        try {
            Sentry.init(
                OptionsContainer.create(SentryOhosOptions.class),
                options -> {
                    OhosOptionsInitializer.init(options, context, logger);
                    configuration.configure(options);
                }, true);
        } catch (IllegalAccessException e) {
            logger.log(SentryLevel.FATAL, "Fatal error during SentryOhos.init(...)", e);
            // This is awful. Should we have this all on the interface and let the caller deal with these?
            // They mean bug in the SDK.
            throw new RuntimeException("Failed to initialize Sentry's SDK", e);
        } catch (InstantiationException e) {
            logger.log(SentryLevel.FATAL, "Fatal error during SentryOhos.init(...)", e);
            throw new RuntimeException("Failed to initialize Sentry's SDK", e);
        } catch (NoSuchMethodException e) {
            logger.log(SentryLevel.FATAL, "Fatal error during SentryOhos.init(...)", e);
            throw new RuntimeException("Failed to initialize Sentry's SDK", e);
        } catch (InvocationTargetException e) {
            logger.log(SentryLevel.FATAL, "Fatal error during SentryOhos.init(...)", e);
            throw new RuntimeException("Failed to initialize Sentry's SDK", e);
        }
    }
}
